﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utils.JqGrid;
using Prerequisites.Models;

namespace Prerequisites.Controllers
{
    public class GridController : Controller
    {
        static List<GridModel> _model;
        static int[] amountValues = { 10, 20, 30, 40 };
        static Dictionary<int, List<GridModelDetail>> _details;

        public ActionResult Index()
        {
            _model = new List<GridModel>();
            _details = new Dictionary<int, List<GridModelDetail>>();

            for (int i = 0; i < 100; i++)
            {
                _model.Add(new GridModel
                {
                    idd = i + 1,
                    invdate = DateTime.Today.AddDays(-(i % 2 == 0 ? i : i - 1)),
                    name = "Client " + (i % 2 == 0 ? i : i - 1),
                    amount = amountValues[i % 4],
                    tax = (i + 1) * 10 + 0.10m,
                    total = (amountValues[i % 4] + ((i + 1) * 10 + 0.10m)),
                    note = i % 2 == 0 ? "livrare" : "incasare"
                });

                List<GridModelDetail> list = new List<GridModelDetail>();

                for (int j = 0; j < 100; j++)
                {
                    list.Add(new GridModelDetail
                    {
                        idd = i + 1,
                        idd2 = j + 1,
                        invdate = DateTime.Today.AddDays(-(i % 2 == 0 ? i : i - 1)),
                        name = "Client2 " + (i % 2 == 0 ? i : i - 1),
                        amount = amountValues[i % 4],
                        tax = (i + 1) * 10 + 0.10m,
                        total = (amountValues[i % 4] + ((i + 1) * 10 + 0.10m)),
                        note = i % 2 == 0 ? "livrare2" : "incasare2"
                    });
                }

                _details.Add(i + 1, list);
            }

            return View();
        }

        public JsonResult GetData(GridSettings grid)
        {
            List<GridModel> list = grid.SearchAndSort(_model.AsQueryable()).ToList();

            var jsonData = new
            {
                total = list.Count / grid.PageSize,
                page = grid.PageIndex,
                records = grid.PageSize,
                rows = list.Skip((grid.PageIndex - 1) * grid.PageSize).Take(grid.PageSize).ToArray(),
                userdata = new
                {
                    name = "Totals:",
                    amount = _model.Sum(m => m.amount),
                    tax = _model.Sum(m => m.tax),
                    total = _model.Sum(m => m.total)
                }
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDataDetails(GridSettings grid, int id)
        {
            List<GridModelDetail> list = grid.SearchAndSort(_details[id].AsQueryable()).ToList();

            var jsonData = new
            {
                total = list.Count / grid.PageSize,
                page = grid.PageIndex,
                records = grid.PageSize,
                rows = list.Skip((grid.PageIndex - 1) * grid.PageSize).Take(grid.PageSize),
                userdata = new
                {
                    name = "Totals:",
                    amount = list.Sum(m => m.amount),
                    tax = list.Sum(m => m.tax),
                    total = list.Sum(m => m.total)
                }
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public ContentResult GetAmountValues(bool noAll = false)
        {
            string select = "<select>";
            if (!noAll)
                select += "<option value=\"\">All</option>";
            string option = "<option value=\"{0}\">{0}</option>";

            for (int i = 0; i < amountValues.Count(); i++)
                select += string.Format(option, amountValues[i]);

            select += "</select>";

            return Content(select);
        }

        public ContentResult Edit(GridModel model, int? id, string oper)
        {
            switch (oper)
            {
                case "edit":
                    return Edit(model, id.Value);
                case "add":
                    return Add(model);
                case "del":
                    return Delete(id.Value);
                default:
                    return Content("bad");
            }

        }

        private ContentResult Edit(GridModel model, int id)
        {
            if (ModelState.IsValid)
            {
                var mm = _model.Single(m => m.idd == id);

                mm.amount = model.amount;
                mm.invdate = model.invdate;
                mm.name = model.name;
                mm.note = model.note;
                mm.tax = model.tax;
                mm.total = model.total;

                return Content("ok");
            }
            else
                return Content("bad");
        }

        private ContentResult Add(GridModel model)
        {
            if (ModelState.IsValid)
            {
                model.idd = _model.Max(i => i.idd) + 1;

                _model.Add(model);

                return Content("ok");
            }
            else
                return Content("bad");
        }

        private ContentResult Delete(int id)
        {
            _model.Remove(_model.Single(m => m.idd == id));

            return Content("ok");
        }
    }
}
